package dao.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import model.AsistentSocial;
import model.Voluntar;
import dao.DaoException;
import dao.VoluntarDao;

public class VoluntarDaoImpl extends Dao implements VoluntarDao {

	@Override
	public void addOrUpdateVoluntar(Voluntar voluntar) {
		hibernateTemplate.saveOrUpdate(voluntar);
		
	}

	@Override
	public Set<Voluntar> getAllVoluntari() {
		@SuppressWarnings("unchecked")
		List<Voluntar> res= hibernateTemplate.loadAll(Voluntar.class);
		return new HashSet<Voluntar>(res);
	}

	@Override
	public void removeVoluntar(int id) throws DaoException {
		hibernateTemplate.delete(this.getVoluntarByID(id));
		
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Voluntar getVoluntarByID(int id) throws DaoException {
		DetachedCriteria crit = DetachedCriteria.forClass(Voluntar.class).add(Restrictions.eq("id", id));
        List res = hibernateTemplate.findByCriteria(crit);
        if (res.size() == 0) {
            throw new DaoException("Nu s-a gasit nici un asistent cu acest id");
        }
       
        return (Voluntar) res.get(0);
	}

	@Override
	public int getMaxId() {
		List res= hibernateTemplate.find("select max(voluntarId) from Voluntar");
		if( res.get(0) == null || res.isEmpty() ) return 0;
		return Integer.parseInt(res.get(0).toString());
	}

}
